
package com.ifcar.app.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ifcar.app.bean.PhotosetListInfo;
import com.ifcar.app.model.Topic;
import com.ifcar.app.util.BaseImageDownloader;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.FitImageView;
import com.ifcar.app.activity.R;

public class TopicAdapter extends BaseAdapter {
    private Context mContext;

    private List<Map<String, Object>> mList;

    private LayoutInflater mInflater;

    private BaseImageDownloader mImageDownloader = null;

    private List<Map<String, Object>> mInfosItem = new ArrayList<Map<String,Object>>();

    private float mRatio;
    
    private ThemeSettingsHelper mThemeSettingsHelper;

    public TopicAdapter(Context c, List<Map<String, Object>> list, BaseImageDownloader downloader) {
        super();
        mContext = c;
        mImageDownloader = downloader;
        mList = list;
        mRatio = ((float)146) / 224;
        mInflater = LayoutInflater.from(c);
        mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(c);

    }

    @Override
    public int getCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }

        return (mList.size() + 1) / 2;
    }

    @Override
    public List<Map<String, Object>> getItem(int index) {
        mInfosItem.clear();
        
        index += index;
        if (index < mList.size()) {
            mInfosItem.add(mList.get(index));
        }
        index++;
        if (index < mList.size()) {
            mInfosItem.add(mList.get(index));
        }
        return mInfosItem;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.topic_item_layout, parent, false);
            holder.itemHolder1.item = convertView.findViewById(R.id.item1);
            holder.itemHolder1.image = (FitImageView) holder.itemHolder1.item
                    .findViewById(R.id.image);
            holder.itemHolder1.image.setRatio(mRatio);
            holder.itemHolder1.name = (TextView) holder.itemHolder1.item
                    .findViewById(R.id.name);
            holder.itemHolder2.item = convertView.findViewById(R.id.item2);
            holder.itemHolder2.image = (FitImageView) holder.itemHolder2.item
                    .findViewById(R.id.image);
            holder.itemHolder2.image.setRatio(mRatio);
            holder.itemHolder2.name = (TextView) holder.itemHolder2.item
                    .findViewById(R.id.name);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        List<Map<String, Object>> infos = getItem(position);
        Map<String, Object> info = infos.size() > 0 ? infos.get(0) : null;
        fillData(info, holder.itemHolder1);
        info = infos.size() > 1 ? infos.get(1) : null;
        fillData(info, holder.itemHolder2);

        return convertView;
    }

    private void fillData(Map<String, Object> info, ItemViewHolder holder) {
        holder.item.setVisibility(View.INVISIBLE);
        // holder.item.setTag(info);

        if (info == null) {
            return;
        } else {
            holder.item.setVisibility(View.VISIBLE);
            holder.name.setText((String)ModelUtils.getValue(info, Topic.PARAM_NAME));
            holder.image.setImageDrawable(null);

            mThemeSettingsHelper.setTextViewColor(mContext, holder.name, R.color.top_text_color);

//            int width = mImageWidth - holder.image.getPaddingLeft()
//                    - holder.image.getPaddingRight();
//            int height = mImageHeight - holder.image.getPaddingTop()
//                    - holder.image.getPaddingBottom();
//            mImageDownloader.setBitmapWandH(width, height, "");
//            mImageDownloader.download((String) ModelUtils.getValue(info, Topic.PARAM_IMGSRC),
//                    holder.image);
            Object imgsrc = ModelUtils.getValue(info, Topic.PARAM_IMGSRC);
            if (imgsrc instanceof Integer) {
                holder.image.setImageResource((Integer)imgsrc);
            } else {
                holder.image.download(mImageDownloader, (String) imgsrc);
            }
            
        }

    }

    private class ViewHolder {
        ItemViewHolder itemHolder1 = new ItemViewHolder();

        ItemViewHolder itemHolder2 = new ItemViewHolder();
    }

    private class ItemViewHolder {
        View item;

        FitImageView image;

        TextView name;
    }
}
